/*
 * Copyright (c) 2011 Team A CS2340. All rights reserved.
 */

// TODO re-enable audit
// $codepro.audit.disable
package edu.gatech.teama.doctorsoffice.client.presenter;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import edu.gatech.teama.doctorsoffice.client.view.AddUserDialog;
import edu.gatech.teama.doctorsoffice.client.view.UserLookupPanel;
import edu.gatech.teama.doctorsoffice.model.Doctor;
import edu.gatech.teama.doctorsoffice.model.Nurse;
import edu.gatech.teama.doctorsoffice.model.SystemAdmin;
import edu.gatech.teama.doctorsoffice.model.User;
/**
 * UserLookupPresenter.java
 * @author unknown
 * @version 1.0
 */
public class UserLookupPresenter {
	/**
	 * list of users
	 */
	private List<User> userlist;
	/**
	 * searched user list
	 */
	private List<User> searchedUserlist;
	
	private UserLookupPanel panel;
	
	private String currentSearch = "";
	
	/**
	 * user lookup presenter constructor
	 */
	public UserLookupPresenter(UserLookupPanel panel) {
		this.fireUserListChanged();
		
		this.panel = panel;
	}
	
	/**
	 * getUserAtTableRow
	 * @param row
	 * @return searchedUserList.get(row);
	 */
	public User getUserAtTableRow(int row) {
		return searchedUserlist.get(row);
	}
	
	/**
	 * getNumberOfVisibleUsers
	 * @return searchedUserlist.size()
	 */
	public int getNumberOfVisibleUsers() {
		return searchedUserlist.size();
	}
	
	/**
	 * look up hte patient
	 * @param search
	 */
	public void lookupPatient(String search) {
		currentSearch = search;
		
		if (search.trim().equals("")) {
			searchedUserlist = new ArrayList<User>(userlist);
			return;
		}
		
		searchedUserlist = new ArrayList<User>();
		
		
		for (User u : userlist) {
			StringTokenizer searchTokens = new StringTokenizer(search);
			String queriedString = u.getFirstName() + " " + u.getLastName() + " " + u.getUsername();
			queriedString = queriedString.toLowerCase();
			
			while (searchTokens.hasMoreTokens()) {
				String nextToken = searchTokens.nextToken();
				if (queriedString.contains(nextToken.toLowerCase()) && !searchedUserlist.contains(u))
					searchedUserlist.add(u);
			}
			
		}
	}
	
	/**
	 * open user at list index
	 * @param index
	 */
	public void openUserAtListIndex(int index) {
		
	}
	
	/**
	 * adds new user
	 */
	public void addNewUser() {
		AddUserDialog dialog = new AddUserDialog();
		dialog.setModal(true);
		dialog.setVisible(true);
		System.out.println("Add User Dialog closed");
		//AddUserDialog.main(null);
	}
	
	
	public User userSelected(int index) {
		if (index < 0)
			return null;
		
		User selectedUser = searchedUserlist.get(index);
		String address_1 = null;
		String address_2 = null;
		
		if (selectedUser.getAddress() != null) {
			address_1 = selectedUser.getAddress().getLine1();
			address_2 = selectedUser.getAddress().getLine2();
		}
		
		panel.setSelectedUserInfo(selectedUser.getFirstName() + " " + selectedUser.getLastName(), address_1, address_2,
				selectedUser.getPhoneNumber(), selectedUser.getEmailAddress());
		
		return selectedUser;
	}
	
	public User userSelected(User selected) {
		return this.userSelected(searchedUserlist.indexOf(selected));
	}
	
	public User userAtIndex(int index) {
		return searchedUserlist.get(index);
	}
	
	public void fireUserListChanged() {
		ServerConnection sc = ServerConnection.getInstance();
		if (sc.getLoggedInUser() instanceof Doctor || sc.getLoggedInUser() instanceof Nurse)
			userlist = sc.getUsersOfTypes("Patient");
		else if (sc.getLoggedInUser() instanceof SystemAdmin)
			userlist = sc.getUsersOfTypes("Patient", "Doctor", "Nurse", "SystemAdmin");
		else
			userlist = new ArrayList<User>();
		
		lookupPatient(currentSearch);
	}
}
